use "C:\'your directory'''\Com01012018_WGS84.dta"
**NB. you should have also the shapefiles in the same directory


***Maps
*Figure 2
grmap invdeltapd, ndsize(none) osize(none none none none none none none none none) cln(9)

*Figure A.1
grmap unemp, ndsize(none) osize(none none none none none none none none none) cln(9) 
grmap ratiofor20182017, ndsize(none) osize(none none none none none none none none none) cln(9) 


* Table 1
reg pdpct2018 pdpct2013 unemp deltaturnout, cluster( prov18 )
reg pdpct2018 pdpct2013 unemp growth deltaturnout, cluster( prov18 )
reg pdpct2018 pdpct2013 unemp growth ratiofor20182017 deltaturnout, cluster( prov18 )


* Table A.4
reg pdpct2018 pdpct2013 deltaunemp deltaturnout, cluster( prov18 )
reg pdpct2018 pdpct2013 deltaunemp growth deltaturnout, cluster( prov18 )
reg pdpct2018 pdpct2013 deltaunemp growth ratiofor20182017 deltaturnout, cluster( prov18 )


* Table A.6

reg pdpct2018 pdpct2013 unemp growth ratiofor20182013 deltaturnout, cluster( prov18 )


* Interactions (Fig. 3 - Tab A.7 - Fig. A2)

reg pdpct2018 pdpct2013 c.ratiofor20182017##c.unemp growth deltaturnout, cluster ( prov18 )

margins, dydx(ratiofor20182017) at(unemp =(3(0.25)28)) vsquish
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash))legend(off) scheme(s2mono) graphregion(fcolor(white)) xsca(titlegap(2)) ///
addplot(hist unemp, percent yaxis(2) bin(20) lwidth(vvthin) color(none) lcolor(gs10) yscale(alt axis(2)))  ///
yline(0, lcolor(red)) ytitle("Marginal Effect of INCREASE FOREIGN", axis(1)) title("") xtitle(UNEMPLOYMENT) title("")

margins, dydx(unemp) at(ratiofor20182017 =(0.5(0.05)1.9)) vsquish
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash))legend(off) scheme(s2mono) graphregion(fcolor(white)) xsca(titlegap(2)) ///
addplot(hist ratiofor20182017 if ratiofor20182017<1.9 & ratiofor20182017>0.5, percent yaxis(2) bin(20) lwidth(vvthin) color(none) lcolor(gs10) yscale(alt axis(2)))  ///
yline(0, lcolor(red)) ytitle("Marginal Effect of UNEMPLOYMENT", axis(1)) title("") xtitle(INCREASE FOREIGN) title("")


reg pdpct2018 pdpct2013 c.ratiofor20182013##c.unemp growth deltaturnout, cluster ( prov18 )

margins, dydx(ratiofor20182013) at(unemp =(3(0.25)28)) vsquish
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash))legend(off) scheme(s2mono) graphregion(fcolor(white)) xsca(titlegap(2)) ///
addplot(hist unemp, percent yaxis(2) bin(20) lwidth(vvthin) color(none) lcolor(gs10) yscale(alt axis(2)))  ///
yline(0, lcolor(red)) ytitle("Marginal Effect of INCREASE FOREIGN 2013-18", axis(1)) title("") xtitle(UNEMPLOYMENT (municipal)) title("")

margins, dydx(unemp) at(ratiofor20182013 =(0(0.05)3)) vsquish
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash))legend(off) scheme(s2mono) graphregion(fcolor(white)) xsca(titlegap(2)) ///
addplot(hist ratiofor20182013 if ratiofor20182013<3 & ratiofor20182013>0, percent yaxis(2) bin(20) lwidth(vvthin) color(none) lcolor(gs10) yscale(alt axis(2)))  ///
yline(0, lcolor(red)) ytitle("Marginal Effect of UNEMPLOYMENT", axis(1)) title("") xtitle(INCREASE FOREIGN 2013-18(municipal)) title("")


*Table 2

*SPMatrix
spmatrix create idistance Idistnorm, normalize (row) replace
spmatrix create contiguity Wnorm, normalize (row) replace

spmatrix summarize #spmatname

*Save Spmatrix (stored with the .stswm suffix)
spmatrix save #spmatname using #filename

*Restore/use saved Spmatrix
spmatrix use Idistnorm using Idistnorm
spmatrix use Wnorm using Wnorm

*SPRegress
spregress pdpct2018 pdpct2013 unemp growth ratiofor20182017 deltaturnout, gs2sls heteroskedastic ivarlag(Wnorm:unemp growth ratiofor20182017) force
estat impact

spregress pdpct2018 pdpct2013 unemp growth ratiofor20182017 deltaturnout, gs2sls heteroskedastic ivarlag(Idistnorm:unemp growth ratiofor20182017) force
estat impact

